package io.lkt.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.Query;
import io.lkt.commons.dynamic.datasource.annotation.DataSource;
import io.lkt.modules.sys.dao.BArticleTempDao;
import io.lkt.modules.sys.dao.BArticleTempTypeDao;
import io.lkt.modules.sys.entity.BArticleTempEntity;
import io.lkt.modules.sys.service.BArticleTempService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("bArticleTempService")
public class BArticleTempServiceImpl extends ServiceImpl<BArticleTempDao, BArticleTempEntity> implements BArticleTempService {

    @Autowired
    BArticleTempDao bArticleTempDao;

    @Autowired
    BArticleTempTypeDao bArticleTempTypeDao;

    @Override
    @DataSource("master")
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<BArticleTempEntity> queryWrapper = new QueryWrapper();
        System.out.println(params.get("articleTypeId"));
        System.out.println(params.get("articleTitle"));
        if (params.get("articleTypeId") != null && ((String) params.get("articleTypeId")).trim().length() > 0 && !params.get("articleTypeId").equals("-1")) {
            queryWrapper.eq("article_type_id", params.get("articleTypeId"));
        }

        if (params.get("articleTitle") != null && !((String) params.get("articleTitle")).trim().equals("")) {
            queryWrapper.like("article_title", params.get("articleTitle"));
//            queryWrapper.eq("article_title", params.get("articleTitle"));
        }

        queryWrapper.select("id", "article_type_id", "article_title", "status", "create_by", "create_time");

        IPage<BArticleTempEntity> page = this.page(
                new Query<BArticleTempEntity>().getPage(params),
                queryWrapper.orderByDesc("order_num", "id")
        );

        for (BArticleTempEntity info : page.getRecords()) {
            if (info.getStatus() != null) {
                info.setStatusName(info.getStatus() == 1 ? "正常" : "删除");
            }

            if (info.getArticleTypeId() != null && info.getArticleTypeId() >= 10) {
                info.setArticleTypeName(bArticleTempTypeDao.selectById(info.getArticleTypeId()).getTypeName());
            }
        }

        return new PageUtils(page);
    }

    @Override
    @DataSource("master")
    public List<BArticleTempEntity> selectList(Integer typeId) {
        QueryWrapper<BArticleTempEntity> queryWrapper = new QueryWrapper();

        queryWrapper.eq("article_type_id", typeId);
        queryWrapper.eq("status", 1);

        List<BArticleTempEntity> list = bArticleTempDao.selectList(queryWrapper);

        return list;
    }

    @Override
    @DataSource("master")
    public List<BArticleTempEntity> selectList(BArticleTempEntity bArticleTempEntity) {
        QueryWrapper<BArticleTempEntity> queryWrapper = new QueryWrapper();

        if (bArticleTempEntity != null && bArticleTempEntity.getArticleTypeId() != null && bArticleTempEntity.getArticleTypeId() != -1) {
            queryWrapper.eq("article_type_id", bArticleTempEntity.getArticleTypeId());
        }

        if (bArticleTempEntity != null && bArticleTempEntity.getArticleTitle() != null && bArticleTempEntity.getArticleTitle().length() > 0) {
            queryWrapper.like("article_title", bArticleTempEntity.getArticleTitle());
        }

        queryWrapper.eq("status", 1);
        queryWrapper.select("id", "article_type_id", "article_title", "status", "create_by", "create_time");

        List<BArticleTempEntity> list = bArticleTempDao.selectList(queryWrapper);

        for (BArticleTempEntity info : list) {
            if (info.getStatus() != null) {
                info.setStatusName(info.getStatus() == 1 ? "正常" : "删除");
            }

            if (info.getArticleTypeId() != null && info.getArticleTypeId() >= 10) {
                info.setArticleTypeName(bArticleTempTypeDao.selectById(info.getArticleTypeId()).getTypeName());
            }
        }

        return list;
    }

    @Override
    @DataSource("master")
    public BArticleTempEntity getById(Integer id) {
        return bArticleTempDao.selectById(id);
    }

    @Override
    @DataSource("master")
    public boolean save(BArticleTempEntity entity) {
        return this.retBool(bArticleTempDao.insert(entity));
    }

    @Override
    @DataSource("master")
    public boolean updateById(BArticleTempEntity entity) {
        return this.retBool(bArticleTempDao.updateById(entity));
    }

}
